<?php

namespace Index\Controller;

class ArticleController extends CommonController {

    protected $Article;
    protected $Book;
    protected $book_id;
    protected $bookInfo;

    public function __construct(){
        parent::__construct();
        $this->book_id = I('request.book_id');
        $this->Book = new \Common\Model\BookModel();
        if($this->book_id > 0 ){
            $info = $this->Book->getList("`book_id` = '{$this->book_id}'");
        }
        if(empty($info['lists'][0])){
            $this->error('页面不存在');exit;
        }
        $this->Book->where("`book_id` = '{$this->book_id}'")->setInc('brow_num');//浏览量+1
        $this->bookInfo = $info['lists'][0];
        $this->assign('bookInfo',$this->bookInfo);
        $this->Article = new \Common\Model\BookArticleModel();
    }

    //章节列表
    public function index(){
        $bookName = $this->bookInfo['book_name'];
        $this->_pageTitle = $bookName.'最新章节_'.$bookName.'无弹窗全文阅读_八九六中文';
        $this->_pageKeywords = $bookName.','.$bookName.'最新章节';
        $this->_pageDescription = $bookName.'最新章节由网友提供，《'.$bookName.'》情节跌宕起伏、扣人心弦，是一本情节与文笔俱佳的，八九六中文免费提供'.$bookName.'最新清爽干净的文字章节在线阅读。';

        //获取最新章节
        $news_article = $this->Article->getNewArticle($this->book_id);
        $this->assign('new',$news_article);
        //推荐书籍
        $book_tj = $this->Book->getTjBookOfCate($this->bookInfo['cate_id']);
        $this->assign('book_tj',$book_tj);
        //章节列表
        $articles = $this->Article->where("`book_id` = '{$this->book_id}'")->order('year asc,month asc,day asc,article_id asc')->select();
        $this->assign('articles',$articles);
        $this->assign('seo360',true);
        $this->display('Index/article');
    }


    //章节内容
    public function content(){
        $article_id = I('request.article_id');
        if($article_id == 0){
            $this->index();exit;
        }
        $where = "`article_id`='{$article_id}' AND `book_id`='{$this->book_id}'";
        if(!$this->Article->where($where)->find() || empty($this->book_id)){
            $this->error('页面不存在');exit;
        }
        //热门推荐
        $ids = \Common\Model\SettingModel::getValue('hot-tj');
        $jindTj = D('Common/Book')->where("book_id IN ({$ids})")->field('book_id,book_name')->limit(13)->select();


        $articleInfo = $this->Article->nextPrevArticle($article_id,$this->book_id);

        $bookName = $this->bookInfo['book_name'];
        $articleName = $articleInfo['article_name'];

        $this->_pageTitle = $articleName.'_'.$bookName.'_'.$bookinfo['lists'][0]['cate_name'].'_八九六中文';
        $this->_pageKeywords = $bookName.','.$articleName;
        $this->_pageDescription = '八九六中文提供了'.$this->bookInfo['author_name'].'创作的玄幻小说《'.$bookName.'》干净清爽无错字的文字章节： '.$articleName.'在线阅读。';

        $content = file_get_contents('.'.$articleInfo['content']);

        $this->assign('hotTj',$jindTj);
        $this->assign('article_id',$article_id);
        $this->assign('articleInfo',$articleInfo);
        $this->assign('content',$content);
        $this->display('Index/content');
    }

    //章节报错，
    public function sendErrorArticle(){
        $article_id = I('post.article_id');
        $book_id = I('post.book_id');
        $message = I('post.message');
        if(IS_AJAX && $message && $book_id > 0){
            $data = array(
                    'send_time'=>time(),
                    'send_ip'=>get_client_ip(),
                    'article_id'=>empty($article_id)?0:$article_id,
                    'book_id'=>empty($book_id)?0:$book_id,
                    'member_id'=>empty($this->_id)?0:$this->_id,
                    'type'=>'err',
                    'message'=>$message.' 章节报错',
                );
            $Message = new \Common\Model\MessageModel();

            if($Message->checkSendOk($data)){
                if($Message->add($data)){
                    \Common\Library\COM\MemberDo::addMemberPoint($this->_id,10,'举报章节内容错误');
                    exit('success');
                }
            }
            p($Message->getLastSql());
        }
    }











}